package com.icangya.DAO.impl;

import com.icangya.DAO.IProxyLevelDAO;
import com.icangya.domain.ProxyLevel;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by lyt on 2017/6/1.
 */
@Repository
public class ProxyLevelDAOImpl extends BaseDAO<ProxyLevel> implements IProxyLevelDAO {
    @Override
    public ProxyLevel getSuperiorUserByUid(Long uid) {
        String hql = "FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.uid = ? ";
        List list = getHibernateTemplate().find(hql, uid);
        if (list != null && list.size() > 0)
            return (ProxyLevel) list.get(0);
        return null;
    }

    @Override
    public ProxyLevel getSuperiorUserByOpenId(String openId) {
        String hql = "FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.openId = ? ";
        List list = getHibernateTemplate().find(hql, openId);
        if (list != null && list.size() > 0)
            return (ProxyLevel) list.get(0);
        return null;
    }

    @Override
    public List getProxyLevel() {

        String hql = "FROM ProxyLevel proxyLevelDO ";
        List list = getHibernateTemplate().find(hql);
        if (list != null && list.size() > 0)
            return list;
        return null;
    }


    @Override
    public Long getFirstCount(Long uid) {
        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.firstLevel = ? GROUP BY proxyLevelDO.firstLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);
    }

    @Override
    public Long getSecondCount(Long uid) {
        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.secondLevel = ? GROUP BY proxyLevelDO.secondLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);
    }

    @Override
    public Long getThirdCount(Long uid) {
        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.thirdLevel = ? GROUP BY proxyLevelDO.thirdLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);
    }

    @Override
    public Long getFourthCount(Long uid) {

        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.fourthLevel = ? GROUP BY proxyLevelDO.fourthLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);
    }

    @Override
    public Long getFiveCount(Long uid) {

        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.fiveLevel = ? GROUP BY proxyLevelDO.fiveLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);
    }

    @Override
    public Long getSixCount(Long uid) {

        String hql = "select count(proxyLevelDO) FROM ProxyLevel proxyLevelDO WHERE proxyLevelDO.sixLevel = ? GROUP BY proxyLevelDO.sixLevel";
//        List list = getHibernateTemplate().find(hql, uid);
//        if (list == null || list.size() == 0)
//            return 0L;
//        Long value = (Long) list.get(0);
//        return value;
        return getLevelCount(hql,uid);

    }

    Long getLevelCount(String hql,long uid){
        List list = getHibernateTemplate().find(hql, uid);
        if (list == null || list.size() == 0)
            return 0L;
        Long value = (Long) list.get(0);
        return value;
    }
}
